
// A1_Merge_trade_and_production
//==============================================================================

// Description: This file merges trade and production data and converts both to 
// the same units if necessary and possible

clear
eststo clear
set more off
cd "D:\data_replication"

//==============================================================================


// 2003, Quarter 1 
//==============================================================================

// Match Trade and Production Data
//------------------------------------------------------------------------------

use data\trade\imports_quarter_pc8_1_2003.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2003.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner


// Assign numbers to quantity units used
//------------------------------------------------------------------------------

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"


// Bring trade and production numbers to matching decimals
//------------------------------------------------------------------------------

replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 100 if unit_trade == 1
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


// Test Quality of the Match via Regression
//------------------------------------------------------------------------------

gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)

save estimation\1_data_format\data_1_2003.dta, replace
sort prccode year quarter declarant
by prccode year quarter declarant: keep if _n == 1 
regress log_C_imports_quarter log_imports_comstats, vce(cluster declarant)
eststo: regress log_C_imports_quarter log_imports_comstats, noconstant vce(cluster declarant)

esttab using estimation/1_data_format/supplemental_files/reg_match.tex, wide r2 label replace ///
title(Regression - Reported versus inferred Imports by PC8)
eststo clear

scatter log_C_imports_quarter log_imports_comstats
graph export estimation/1_data_format/supplemental_files/reg_match.png, replace


// Adjust imports to match aggregate numbers reported by prodcom
//------------------------------------------------------------------------------

use estimation\1_data_format\data_1_2003.dta, clear
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 
save estimation\1_data_format\data_1_2003.dta, replace


// Keep only Imports when trade & production units match
//------------------------------------------------------------------------------

gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_1_2003.dta, replace

// Create List of remaining PC8
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_1_2003.dta, replace


// Keep only Production when trade & production units match
//------------------------------------------------------------------------------

use estimation\1_data_format\data_1_2003.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_1_2003.dta, replace


// Manually Match units and keep Imports for those
//------------------------------------------------------------------------------

use estimation\1_data_format\data_1_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_1_2003.dta, replace

// Create List of manually fixed PC8
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_1_2003.dta, replace


// Manually Match units and keep Production for those
//------------------------------------------------------------------------------

use estimation\1_data_format\data_1_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_1_2003.dta, replace


// Create List of PC8 with non-matching Units
//------------------------------------------------------------------------------

use estimation\1_data_format\data_1_2003.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_1_2003.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_1_2003.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_1_2003.dta, replace


// Create Final Dataset
//------------------------------------------------------------------------------

use estimation\1_data_format\imports_first_1_2003.dta, clear
append using estimation\1_data_format\imports_second_1_2003.dta
append using estimation\1_data_format\production_first_1_2003.dta
append using estimation\1_data_format\production_second_1_2003.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_1_2003.dta, replace


// Delete intermediate files
rm estimation\1_data_format\data_1_2003.dta
rm estimation\1_data_format\imports_first_1_2003.dta
rm estimation\1_data_format\imports_second_1_2003.dta
rm estimation\1_data_format\pc8_first_1_2003.dta
rm estimation\1_data_format\pc8_second_1_2003.dta
rm estimation\1_data_format\pc8_third_1_2003.dta
rm estimation\1_data_format\production_first_1_2003.dta
rm estimation\1_data_format\production_second_1_2003.dta

//==============================================================================


// 2003, Quarter 2
//==============================================================================

use data\trade\imports_quarter_pc8_2_2003.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2003.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"

replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 100 if unit_trade == 1
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_2_2003.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_2_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_2_2003.dta, replace


use estimation\1_data_format\data_2_2003.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_2_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_2_2003.dta, replace



use estimation\1_data_format\data_2_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_2_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_2_2003.dta, replace



use estimation\1_data_format\data_2_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_2_2003.dta, replace


use estimation\1_data_format\data_2_2003.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_2_2003.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_2_2003.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_2_2003.dta, replace


use estimation\1_data_format\imports_first_2_2003.dta, clear
append using estimation\1_data_format\imports_second_2_2003.dta
append using estimation\1_data_format\production_first_2_2003.dta
append using estimation\1_data_format\production_second_2_2003.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_2_2003.dta, replace


rm estimation\1_data_format\data_2_2003.dta
rm estimation\1_data_format\imports_first_2_2003.dta
rm estimation\1_data_format\imports_second_2_2003.dta
rm estimation\1_data_format\pc8_first_2_2003.dta
rm estimation\1_data_format\pc8_first_alt_2_2003.dta
rm estimation\1_data_format\pc8_second_2_2003.dta
rm estimation\1_data_format\pc8_third_2_2003.dta
rm estimation\1_data_format\production_first_2_2003.dta
rm estimation\1_data_format\production_second_2_2003.dta

//==============================================================================


// 2003, Quarter 3 
//==============================================================================


use data\trade\imports_quarter_pc8_3_2003.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2003.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"

replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2

gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_3_2003.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_3_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_3_2003.dta, replace


use estimation\1_data_format\data_3_2003.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_3_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_3_2003.dta, replace



use estimation\1_data_format\data_3_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_3_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_3_2003.dta, replace



use estimation\1_data_format\data_3_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_3_2003.dta, replace


use estimation\1_data_format\data_3_2003.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_3_2003.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_3_2003.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_3_2003.dta, replace


use estimation\1_data_format\imports_first_3_2003.dta, clear
append using estimation\1_data_format\imports_second_3_2003.dta
append using estimation\1_data_format\production_first_3_2003.dta
append using estimation\1_data_format\production_second_3_2003.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_3_2003.dta, replace


rm estimation\1_data_format\data_3_2003.dta
rm estimation\1_data_format\imports_first_3_2003.dta
rm estimation\1_data_format\imports_second_3_2003.dta
rm estimation\1_data_format\pc8_first_3_2003.dta
rm estimation\1_data_format\pc8_first_alt_3_2003.dta
rm estimation\1_data_format\pc8_second_3_2003.dta
rm estimation\1_data_format\pc8_third_3_2003.dta
rm estimation\1_data_format\production_first_3_2003.dta
rm estimation\1_data_format\production_second_3_2003.dta

//==============================================================================


// 2003, Quarter 4 
//==============================================================================

use data\trade\imports_quarter_pc8_4_2003.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2003.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"

replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_4_2003.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_4_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_4_2003.dta, replace


use estimation\1_data_format\data_4_2003.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_4_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_4_2003.dta, replace



use estimation\1_data_format\data_4_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_4_2003.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_4_2003.dta, replace



use estimation\1_data_format\data_4_2003.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 16    //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_4_2003.dta, replace


use estimation\1_data_format\data_4_2003.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_4_2003.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_4_2003.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_4_2003.dta, replace


use estimation\1_data_format\imports_first_4_2003.dta, clear
append using estimation\1_data_format\imports_second_4_2003.dta
append using estimation\1_data_format\production_first_4_2003.dta
append using estimation\1_data_format\production_second_4_2003.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_4_2003.dta, replace


rm estimation\1_data_format\data_4_2003.dta
rm estimation\1_data_format\imports_first_4_2003.dta
rm estimation\1_data_format\imports_second_4_2003.dta
rm estimation\1_data_format\pc8_first_4_2003.dta
rm estimation\1_data_format\pc8_first_alt_4_2003.dta
rm estimation\1_data_format\pc8_second_4_2003.dta
rm estimation\1_data_format\pc8_third_4_2003.dta
rm estimation\1_data_format\production_first_4_2003.dta
rm estimation\1_data_format\production_second_4_2003.dta

//==============================================================================


// 2004, Quarter 1 
//==============================================================================

use data\trade\imports_quarter_pc8_1_2004.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2004.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_1_2004.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_1_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_1_2004.dta, replace


use estimation\1_data_format\data_1_2004.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_1_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_1_2004.dta, replace



use estimation\1_data_format\data_1_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_1_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_1_2004.dta, replace



use estimation\1_data_format\data_1_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_1_2004.dta, replace


use estimation\1_data_format\data_1_2004.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_1_2004.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_1_2004.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_1_2004.dta, replace


use estimation\1_data_format\imports_first_1_2004.dta, clear
append using estimation\1_data_format\imports_second_1_2004.dta
append using estimation\1_data_format\production_first_1_2004.dta
append using estimation\1_data_format\production_second_1_2004.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_1_2004.dta, replace


rm estimation\1_data_format\data_1_2004.dta
rm estimation\1_data_format\imports_first_1_2004.dta
rm estimation\1_data_format\imports_second_1_2004.dta
rm estimation\1_data_format\pc8_first_1_2004.dta
rm estimation\1_data_format\pc8_first_alt_1_2004.dta
rm estimation\1_data_format\pc8_second_1_2004.dta
rm estimation\1_data_format\pc8_third_1_2004.dta
rm estimation\1_data_format\production_first_1_2004.dta
rm estimation\1_data_format\production_second_1_2004.dta

//==============================================================================


// 2004, Quarter 2 
//==============================================================================

use data\trade\imports_quarter_pc8_2_2004.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2004.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_2_2004.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_2_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_2_2004.dta, replace


use estimation\1_data_format\data_2_2004.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_2_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_2_2004.dta, replace



use estimation\1_data_format\data_2_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_2_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_2_2004.dta, replace



use estimation\1_data_format\data_2_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_2_2004.dta, replace


use estimation\1_data_format\data_2_2004.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_2_2004.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_2_2004.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_2_2004.dta, replace


use estimation\1_data_format\imports_first_2_2004.dta, clear
append using estimation\1_data_format\imports_second_2_2004.dta
append using estimation\1_data_format\production_first_2_2004.dta
append using estimation\1_data_format\production_second_2_2004.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_2_2004.dta, replace


rm estimation\1_data_format\data_2_2004.dta
rm estimation\1_data_format\imports_first_2_2004.dta
rm estimation\1_data_format\imports_second_2_2004.dta
rm estimation\1_data_format\pc8_first_2_2004.dta
rm estimation\1_data_format\pc8_first_alt_2_2004.dta
rm estimation\1_data_format\pc8_second_2_2004.dta
rm estimation\1_data_format\pc8_third_2_2004.dta
rm estimation\1_data_format\production_first_2_2004.dta
rm estimation\1_data_format\production_second_2_2004.dta

//==============================================================================


// 2004, Quarter 3 
//==============================================================================

use data\trade\imports_quarter_pc8_3_2004.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2004.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_3_2004.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_3_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_3_2004.dta, replace


use estimation\1_data_format\data_3_2004.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_3_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_3_2004.dta, replace



use estimation\1_data_format\data_3_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_3_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_3_2004.dta, replace



use estimation\1_data_format\data_3_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_3_2004.dta, replace


use estimation\1_data_format\data_3_2004.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_3_2004.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_3_2004.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_3_2004.dta, replace


use estimation\1_data_format\imports_first_3_2004.dta, clear
append using estimation\1_data_format\imports_second_3_2004.dta
append using estimation\1_data_format\production_first_3_2004.dta
append using estimation\1_data_format\production_second_3_2004.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_3_2004.dta, replace


rm estimation\1_data_format\data_3_2004.dta
rm estimation\1_data_format\imports_first_3_2004.dta
rm estimation\1_data_format\imports_second_3_2004.dta
rm estimation\1_data_format\pc8_first_3_2004.dta
rm estimation\1_data_format\pc8_first_alt_3_2004.dta
rm estimation\1_data_format\pc8_second_3_2004.dta
rm estimation\1_data_format\pc8_third_3_2004.dta
rm estimation\1_data_format\production_first_3_2004.dta
rm estimation\1_data_format\production_second_3_2004.dta

//==============================================================================


// 2004, Quarter 4 
//==============================================================================

use data\trade\imports_quarter_pc8_4_2004.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2004.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 41 if unit_production_quantity == "km"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_4_2004.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_4_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_4_2004.dta, replace


use estimation\1_data_format\data_4_2004.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_4_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_4_2004.dta, replace



use estimation\1_data_format\data_4_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_4_2004.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_4_2004.dta, replace



use estimation\1_data_format\data_4_2004.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_4_2004.dta, replace


use estimation\1_data_format\data_4_2004.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_4_2004.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_4_2004.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_4_2004.dta, replace


use estimation\1_data_format\imports_first_4_2004.dta, clear
append using estimation\1_data_format\imports_second_4_2004.dta
append using estimation\1_data_format\production_first_4_2004.dta
append using estimation\1_data_format\production_second_4_2004.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_4_2004.dta, replace


rm estimation\1_data_format\data_4_2004.dta
rm estimation\1_data_format\imports_first_4_2004.dta
rm estimation\1_data_format\imports_second_4_2004.dta
rm estimation\1_data_format\pc8_first_4_2004.dta
rm estimation\1_data_format\pc8_first_alt_4_2004.dta
rm estimation\1_data_format\pc8_second_4_2004.dta
rm estimation\1_data_format\pc8_third_4_2004.dta
rm estimation\1_data_format\production_first_4_2004.dta
rm estimation\1_data_format\production_second_4_2004.dta

//==============================================================================


// 2005, Quarter 1 
//==============================================================================

use data\trade\imports_quarter_pc8_1_2005.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2005.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_1_2005.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_1_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_1_2005.dta, replace


use estimation\1_data_format\data_1_2005.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_1_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_1_2005.dta, replace



use estimation\1_data_format\data_1_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_1_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_1_2005.dta, replace



use estimation\1_data_format\data_1_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_1_2005.dta, replace


use estimation\1_data_format\data_1_2005.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_1_2005.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_1_2005.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_1_2005.dta, replace


use estimation\1_data_format\imports_first_1_2005.dta, clear
append using estimation\1_data_format\imports_second_1_2005.dta
append using estimation\1_data_format\production_first_1_2005.dta
append using estimation\1_data_format\production_second_1_2005.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_1_2005.dta, replace


rm estimation\1_data_format\data_1_2005.dta
rm estimation\1_data_format\imports_first_1_2005.dta
rm estimation\1_data_format\imports_second_1_2005.dta
rm estimation\1_data_format\pc8_first_1_2005.dta
rm estimation\1_data_format\pc8_first_alt_1_2005.dta
rm estimation\1_data_format\pc8_second_1_2005.dta
rm estimation\1_data_format\pc8_third_1_2005.dta
rm estimation\1_data_format\production_first_1_2005.dta
rm estimation\1_data_format\production_second_1_2005.dta

//==============================================================================


// 2005, Quarter 2
//==============================================================================

use data\trade\imports_quarter_pc8_2_2005.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2005.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"

replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_2_2005.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_2_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_2_2005.dta, replace


use estimation\1_data_format\data_2_2005.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_2_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_2_2005.dta, replace



use estimation\1_data_format\data_2_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_2_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_2_2005.dta, replace



use estimation\1_data_format\data_2_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_2_2005.dta, replace


use estimation\1_data_format\data_2_2005.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_2_2005.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_2_2005.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_2_2005.dta, replace


use estimation\1_data_format\imports_first_2_2005.dta, clear
append using estimation\1_data_format\imports_second_2_2005.dta
append using estimation\1_data_format\production_first_2_2005.dta
append using estimation\1_data_format\production_second_2_2005.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_2_2005.dta, replace


rm estimation\1_data_format\data_2_2005.dta
rm estimation\1_data_format\imports_first_2_2005.dta
rm estimation\1_data_format\imports_second_2_2005.dta
rm estimation\1_data_format\pc8_first_2_2005.dta
rm estimation\1_data_format\pc8_first_alt_2_2005.dta
rm estimation\1_data_format\pc8_second_2_2005.dta
rm estimation\1_data_format\pc8_third_2_2005.dta
rm estimation\1_data_format\production_first_2_2005.dta
rm estimation\1_data_format\production_second_2_2005.dta

//==============================================================================


// 2005, Quarter 3 
//==============================================================================

use data\trade\imports_quarter_pc8_3_2005.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2005.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_3_2005.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_3_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_3_2005.dta, replace


use estimation\1_data_format\data_3_2005.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_3_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_3_2005.dta, replace



use estimation\1_data_format\data_3_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_3_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_3_2005.dta, replace



use estimation\1_data_format\data_3_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_3_2005.dta, replace


use estimation\1_data_format\data_3_2005.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_3_2005.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_3_2005.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_3_2005.dta, replace


use estimation\1_data_format\imports_first_3_2005.dta, clear
append using estimation\1_data_format\imports_second_3_2005.dta
append using estimation\1_data_format\production_first_3_2005.dta
append using estimation\1_data_format\production_second_3_2005.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_3_2005.dta, replace


rm estimation\1_data_format\data_3_2005.dta
rm estimation\1_data_format\imports_first_3_2005.dta
rm estimation\1_data_format\imports_second_3_2005.dta
rm estimation\1_data_format\pc8_first_3_2005.dta
rm estimation\1_data_format\pc8_first_alt_3_2005.dta
rm estimation\1_data_format\pc8_second_3_2005.dta
rm estimation\1_data_format\pc8_third_3_2005.dta
rm estimation\1_data_format\production_first_3_2005.dta
rm estimation\1_data_format\production_second_3_2005.dta

//==============================================================================


// 2005, Quarter 4 
//==============================================================================

use data\trade\imports_quarter_pc8_4_2005.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2005.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_4_2005.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_4_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_4_2005.dta, replace


use estimation\1_data_format\data_4_2005.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_4_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_4_2005.dta, replace



use estimation\1_data_format\data_4_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_4_2005.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_4_2005.dta, replace



use estimation\1_data_format\data_4_2005.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_4_2005.dta, replace


use estimation\1_data_format\data_4_2005.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_4_2005.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_4_2005.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_4_2005.dta, replace


use estimation\1_data_format\imports_first_4_2005.dta, clear
append using estimation\1_data_format\imports_second_4_2005.dta
append using estimation\1_data_format\production_first_4_2005.dta
append using estimation\1_data_format\production_second_4_2005.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_4_2005.dta, replace


rm estimation\1_data_format\data_4_2005.dta
rm estimation\1_data_format\imports_first_4_2005.dta
rm estimation\1_data_format\imports_second_4_2005.dta
rm estimation\1_data_format\pc8_first_4_2005.dta
rm estimation\1_data_format\pc8_first_alt_4_2005.dta
rm estimation\1_data_format\pc8_second_4_2005.dta
rm estimation\1_data_format\pc8_third_4_2005.dta
rm estimation\1_data_format\production_first_4_2005.dta
rm estimation\1_data_format\production_second_4_2005.dta

//==============================================================================


// 2006, Quarter 1 
//==============================================================================

use data\trade\imports_quarter_pc8_1_2006.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2006.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_1_2006.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_1_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_1_2006.dta, replace


use estimation\1_data_format\data_1_2006.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_1_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_1_2006.dta, replace



use estimation\1_data_format\data_1_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_1_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_1_2006.dta, replace



use estimation\1_data_format\data_1_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_1_2006.dta, replace


use estimation\1_data_format\data_1_2006.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_1_2006.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_1_2006.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_1_2006.dta, replace


use estimation\1_data_format\imports_first_1_2006.dta, clear
append using estimation\1_data_format\imports_second_1_2006.dta
append using estimation\1_data_format\production_first_1_2006.dta
append using estimation\1_data_format\production_second_1_2006.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_1_2006.dta, replace


rm estimation\1_data_format\data_1_2006.dta
rm estimation\1_data_format\imports_first_1_2006.dta
rm estimation\1_data_format\imports_second_1_2006.dta
rm estimation\1_data_format\pc8_first_1_2006.dta
rm estimation\1_data_format\pc8_first_alt_1_2006.dta
rm estimation\1_data_format\pc8_second_1_2006.dta
rm estimation\1_data_format\pc8_third_1_2006.dta
rm estimation\1_data_format\production_first_1_2006.dta
rm estimation\1_data_format\production_second_1_2006.dta

//==============================================================================


// 2006, Quarter 2
//==============================================================================

use data\trade\imports_quarter_pc8_2_2006.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2006.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_2_2006.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_2_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_2_2006.dta, replace


use estimation\1_data_format\data_2_2006.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_2_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_2_2006.dta, replace



use estimation\1_data_format\data_2_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_2_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_2_2006.dta, replace



use estimation\1_data_format\data_2_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_2_2006.dta, replace


use estimation\1_data_format\data_2_2006.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_2_2006.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_2_2006.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_2_2006.dta, replace


use estimation\1_data_format\imports_first_2_2006.dta, clear
append using estimation\1_data_format\imports_second_2_2006.dta
append using estimation\1_data_format\production_first_2_2006.dta
append using estimation\1_data_format\production_second_2_2006.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_2_2006.dta, replace


rm estimation\1_data_format\data_2_2006.dta
rm estimation\1_data_format\imports_first_2_2006.dta
rm estimation\1_data_format\imports_second_2_2006.dta
rm estimation\1_data_format\pc8_first_2_2006.dta
rm estimation\1_data_format\pc8_first_alt_2_2006.dta
rm estimation\1_data_format\pc8_second_2_2006.dta
rm estimation\1_data_format\pc8_third_2_2006.dta
rm estimation\1_data_format\production_first_2_2006.dta
rm estimation\1_data_format\production_second_2_2006.dta

//==============================================================================


// 2006, Quarter 3 
//==============================================================================

use data\trade\imports_quarter_pc8_3_2006.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2006.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_3_2006.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_3_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_3_2006.dta, replace


use estimation\1_data_format\data_3_2006.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_3_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_3_2006.dta, replace



use estimation\1_data_format\data_3_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_3_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_3_2006.dta, replace



use estimation\1_data_format\data_3_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_3_2006.dta, replace


use estimation\1_data_format\data_3_2006.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_3_2006.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_3_2006.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_3_2006.dta, replace


use estimation\1_data_format\imports_first_3_2006.dta, clear
append using estimation\1_data_format\imports_second_3_2006.dta
append using estimation\1_data_format\production_first_3_2006.dta
append using estimation\1_data_format\production_second_3_2006.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_3_2006.dta, replace


rm estimation\1_data_format\data_3_2006.dta
rm estimation\1_data_format\imports_first_3_2006.dta
rm estimation\1_data_format\imports_second_3_2006.dta
rm estimation\1_data_format\pc8_first_3_2006.dta
rm estimation\1_data_format\pc8_first_alt_3_2006.dta
rm estimation\1_data_format\pc8_second_3_2006.dta
rm estimation\1_data_format\pc8_third_3_2006.dta
rm estimation\1_data_format\production_first_3_2006.dta
rm estimation\1_data_format\production_second_3_2006.dta

//==============================================================================


// 2006, Quarter 4 
//==============================================================================

use data\trade\imports_quarter_pc8_4_2006.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2006.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_4_2006.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_4_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_4_2006.dta, replace


use estimation\1_data_format\data_4_2006.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_4_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_4_2006.dta, replace



use estimation\1_data_format\data_4_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_4_2006.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_4_2006.dta, replace



use estimation\1_data_format\data_4_2006.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_4_2006.dta, replace


use estimation\1_data_format\data_4_2006.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_4_2006.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_4_2006.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_4_2006.dta, replace


use estimation\1_data_format\imports_first_4_2006.dta, clear
append using estimation\1_data_format\imports_second_4_2006.dta
append using estimation\1_data_format\production_first_4_2006.dta
append using estimation\1_data_format\production_second_4_2006.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_4_2006.dta, replace


rm estimation\1_data_format\data_4_2006.dta
rm estimation\1_data_format\imports_first_4_2006.dta
rm estimation\1_data_format\imports_second_4_2006.dta
rm estimation\1_data_format\pc8_first_4_2006.dta
rm estimation\1_data_format\pc8_first_alt_4_2006.dta
rm estimation\1_data_format\pc8_second_4_2006.dta
rm estimation\1_data_format\pc8_third_4_2006.dta
rm estimation\1_data_format\production_first_4_2006.dta
rm estimation\1_data_format\production_second_4_2006.dta

//==============================================================================


// 2007, Quarter 1 
//==============================================================================

use data\trade\imports_quarter_pc8_1_2007.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2007.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_1_2007.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_1_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_1_2007.dta, replace


use estimation\1_data_format\data_1_2007.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_1_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_1_2007.dta, replace



use estimation\1_data_format\data_1_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_1_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_1_2007.dta, replace



use estimation\1_data_format\data_1_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_1_2007.dta, replace


use estimation\1_data_format\data_1_2007.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_1_2007.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_1_2007.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_1_2007.dta, replace


use estimation\1_data_format\imports_first_1_2007.dta, clear
append using estimation\1_data_format\imports_second_1_2007.dta
append using estimation\1_data_format\production_first_1_2007.dta
append using estimation\1_data_format\production_second_1_2007.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_1_2007.dta, replace


rm estimation\1_data_format\data_1_2007.dta
rm estimation\1_data_format\imports_first_1_2007.dta
rm estimation\1_data_format\imports_second_1_2007.dta
rm estimation\1_data_format\pc8_first_1_2007.dta
rm estimation\1_data_format\pc8_first_alt_1_2007.dta
rm estimation\1_data_format\pc8_second_1_2007.dta
rm estimation\1_data_format\pc8_third_1_2007.dta
rm estimation\1_data_format\production_first_1_2007.dta
rm estimation\1_data_format\production_second_1_2007.dta

//==============================================================================


// 2007, Quarter 2 
//==============================================================================

use data\trade\imports_quarter_pc8_2_2007.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2007.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_2_2007.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_2_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_2_2007.dta, replace


use estimation\1_data_format\data_2_2007.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_2_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_2_2007.dta, replace



use estimation\1_data_format\data_2_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_2_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_2_2007.dta, replace



use estimation\1_data_format\data_2_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_2_2007.dta, replace


use estimation\1_data_format\data_2_2007.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_2_2007.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_2_2007.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_2_2007.dta, replace


use estimation\1_data_format\imports_first_2_2007.dta, clear
append using estimation\1_data_format\imports_second_2_2007.dta
append using estimation\1_data_format\production_first_2_2007.dta
append using estimation\1_data_format\production_second_2_2007.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_2_2007.dta, replace


rm estimation\1_data_format\data_2_2007.dta
rm estimation\1_data_format\imports_first_2_2007.dta
rm estimation\1_data_format\imports_second_2_2007.dta
rm estimation\1_data_format\pc8_first_2_2007.dta
rm estimation\1_data_format\pc8_first_alt_2_2007.dta
rm estimation\1_data_format\pc8_second_2_2007.dta
rm estimation\1_data_format\pc8_third_2_2007.dta
rm estimation\1_data_format\production_first_2_2007.dta
rm estimation\1_data_format\production_second_2_2007.dta

//==============================================================================


// 2007, Quarter 3 
//==============================================================================

use data\trade\imports_quarter_pc8_3_2007.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2007.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_3_2007.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_3_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_3_2007.dta, replace


use estimation\1_data_format\data_3_2007.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_3_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_3_2007.dta, replace



use estimation\1_data_format\data_3_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_3_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_3_2007.dta, replace



use estimation\1_data_format\data_3_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_3_2007.dta, replace


use estimation\1_data_format\data_3_2007.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_3_2007.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_3_2007.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_3_2007.dta, replace


use estimation\1_data_format\imports_first_3_2007.dta, clear
append using estimation\1_data_format\imports_second_3_2007.dta
append using estimation\1_data_format\production_first_3_2007.dta
append using estimation\1_data_format\production_second_3_2007.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_3_2007.dta, replace


rm estimation\1_data_format\data_3_2007.dta
rm estimation\1_data_format\imports_first_3_2007.dta
rm estimation\1_data_format\imports_second_3_2007.dta
rm estimation\1_data_format\pc8_first_3_2007.dta
rm estimation\1_data_format\pc8_first_alt_3_2007.dta
rm estimation\1_data_format\pc8_second_3_2007.dta
rm estimation\1_data_format\pc8_third_3_2007.dta
rm estimation\1_data_format\production_first_3_2007.dta
rm estimation\1_data_format\production_second_3_2007.dta

//==============================================================================


// 2007, Quarter 4 
//==============================================================================

use data\trade\imports_quarter_pc8_4_2007.dta, clear
rename pc8_string prccode
merge m:1 prccode declarant using data\production\production_imports_2007.dta
gen match = "Production and Trade Data"
replace match = "Only Trade Data" if _merge == 1
replace match = "Only Production Data" if _merge == 2
drop _merge
keep if match == "Production and Trade Data"
egen product_id = group(prccode)
sort prccode year quarter declarant partner

gen unit_trade = 0
gen unit_production = 0
replace unit_production = . if unit_production_quantity == ":"
replace unit_trade = 1 if sup_unit == "100 p/st"
replace unit_trade = 2 if sup_unit == "1000 p/st"
replace unit_trade = 2 if sup_unit == "1 000 p/st"
replace unit_trade = 2 if sup_unit == "1Â 000Â p/st"
replace unit_trade = 3 if sup_unit == "GT"
replace unit_production = 3 if unit_production_quantity == "GT"
replace unit_trade = 4 if sup_unit == "c/k"
replace unit_production = 4 if unit_production_quantity == "c/k"
replace unit_trade = 5 if sup_unit == "ce/el"
replace unit_production = 5 if unit_production_quantity == "ce/el"
replace unit_trade = 6 if sup_unit == "ct/l"
replace unit_production = 6 if unit_production_quantity == "ct/l"
replace unit_trade = 7 if sup_unit == "g"
replace unit_production = 7 if unit_production_quantity == "g"
replace unit_trade = 8 if sup_unit == "kg 90% sdt"
replace unit_production = 8 if unit_production_quantity == "kg 90% sdt"
replace unit_trade = 9 if sup_unit == "kg H2O2"
replace unit_production = 9 if unit_production_quantity == "kg H2O2"
replace unit_trade = 10 if sup_unit == "kg K2O"
replace unit_production = 10 if unit_production_quantity == "kg K2O"
replace unit_trade = 11 if sup_unit == "kg KOH"
replace unit_production = 11 if unit_production_quantity == "kg KOH"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_trade = 12 if sup_unit == "kg N"
replace unit_production = 12 if unit_production_quantity == "kg N"
replace unit_trade = 13 if sup_unit == "kg NaOH"
replace unit_production = 13 if unit_production_quantity == "kg NaOH"
replace unit_trade = 14 if sup_unit == "kg P2O5"
replace unit_trade = 14 if sup_unit == "kg P205"
replace unit_production = 14 if unit_production_quantity == "kg P2O5"
replace unit_trade = 15 if sup_unit == "kg met.am."
replace unit_trade = 16 if sup_unit == "kg/net eda"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 17 if sup_unit == "kg C5H14ClNO"
replace unit_trade = 18 if sup_unit == "l"
replace unit_production = 18 if unit_production_quantity == "l"
replace unit_trade = 19 if sup_unit == "l alc. 100%"
replace unit_trade = 19 if sup_unit == "l alc. 100 %"
replace unit_production = 19 if unit_production_quantity == "l alc 100%"
replace unit_trade = 20 if sup_unit == "m"
replace unit_production = 20 if unit_production_quantity == "m"
replace unit_trade = 21 if sup_unit == "m2"
replace unit_production = 21 if unit_production_quantity == "m2"
replace unit_trade = 22 if sup_unit == "m3"
replace unit_production = 22 if unit_production_quantity == "m3"
replace unit_trade = 23 if sup_unit == "p/st"
replace unit_production = 23 if unit_production_quantity == "p/st"
replace unit_trade = 24 if sup_unit == "pa"
replace unit_production = 24 if unit_production_quantity == "pa"
replace unit_production = 25 if unit_production_quantity == "kg Al2O3"
replace unit_production = 26 if unit_production_quantity == "kg B2O3"
replace unit_production = 27 if unit_production_quantity == "kg BaCO3"
replace unit_production = 28 if unit_production_quantity == "kg Cl"
replace unit_production = 29 if unit_production_quantity == "kg F"
replace unit_production = 30 if unit_production_quantity == "kg HCl"
replace unit_production = 31 if unit_production_quantity == "kg HF"
replace unit_production = 32 if unit_production_quantity == "kg K2CO3"
replace unit_production = 33 if unit_production_quantity == "kg Na2CO3"
replace unit_production = 34 if unit_production_quantity == "kg Na2S2O5"
replace unit_production = 35 if unit_production_quantity == "kg PbO"
replace unit_production = 36 if unit_production_quantity == "kg S"
replace unit_production = 37 if unit_production_quantity == "kg SO2"
replace unit_production = 38 if unit_production_quantity == "kg SiO2"
replace unit_production = 39 if unit_production_quantity == "kg TiO2"
replace unit_production = 40 if unit_production_quantity == "kg act.subst"
replace unit_production = 40 if unit_production_quantity == "kg act. subst."
replace unit_production = 41 if unit_production_quantity == "km"
replace unit_production = 42 if unit_production_quantity == "kW"


replace C_production = C_production / 1000
replace C_imports = C_imports / 1000
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 0
replace C_production_quantity = C_production_quantity / 1000 if unit_trade == 2


gen C_imports_quarter = C_imports / 4
sort prccode declarant
by prccode declarant: egen imports_comstats = sum(imports_quarter_ecu_pc8)
gen log_C_imports_quarter = log(C_imports_quarter)
gen log_imports_comstats = log(imports_comstats)
regress log_C_imports_quarter log_imports_comstats, vce(robust)
regress log_C_imports_quarter log_imports_comstats, noconstant vce(robust)
gen adj_factor = C_imports_quarter / imports_comstats
gen imports_quarter_ecu_pc8_adj = imports_quarter_ecu_pc8 * adj_factor
gen quantity_quarter_ton_pc8_adj = quantity_quarter_ton_pc8 * adj_factor
gen quantity_quarter_sup_pc8_adj = quantity_quarter_sup_pc8 * adj_factor 

save estimation\1_data_format\data_4_2007.dta, replace


gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8_adj imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_first_4_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_4_2007.dta, replace


use estimation\1_data_format\data_4_2007.dta, clear
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_first_4_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_first_alt_4_2007.dta, replace



use estimation\1_data_format\data_4_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename imports_quarter_ecu_pc8 imports_value_pc8 
gen imports_quantity_pc8 = 0
replace imports_quantity_pc8 = quantity_quarter_ton_pc8_adj if unit_trade == 0
replace imports_quantity_pc8 = quantity_quarter_sup_pc8_adj if unit_trade != 0
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
save estimation\1_data_format\imports_second_4_2007.dta, replace
sort prccode
by prccode: keep if _n == 1
keep prccode
save estimation\1_data_format\pc8_second_4_2007.dta, replace



use estimation\1_data_format\data_4_2007.dta, replace
gen index_units = 0
replace index_units = 1 if unit_production == unit_trade
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: drop if nr_observations == nr_equal_units
drop index_units nr_observations nr_equal_units
gen index_units = 0
replace index_units = 1 if unit_trade == 1 & unit_production == 23    //100 p/st & p/st
replace index_units = 1 if unit_trade == 2 & unit_production == 23    //1000 p/st & p/st
replace index_units = 1 if unit_trade == 8 & unit_production == 0	  //kg 90% sdt & kg
replace index_units = 1 if unit_trade == 9 & unit_production == 0	  //kg H2O2 & kg
replace index_units = 1 if unit_trade == 10 & unit_production == 0	  //kg K2O & kg
replace index_units = 1 if unit_trade == 11 & unit_production == 0	  //kg KOH & kg
replace index_units = 1 if unit_trade == 12 & unit_production == 0	  //kg N & kg
replace index_units = 1 if unit_trade == 13 & unit_production == 0	  //kg NaOH & kg
replace index_units = 1 if unit_trade == 14 & unit_production == 0	  //kg P2O5 & kg
replace index_units = 1 if unit_trade == 15 & unit_production == 0    //kg met.am. & kg
replace index_units = 1 if unit_trade == 16 & unit_production == 0    //kg/net eda & kg
replace index_units = 1 if unit_trade == 17 & unit_production == 0    //kg & kg C5H14ClNO & kg
replace index_units = 1 if unit_trade == 0 & unit_production == 8     //kg & kg 90% sdt
replace index_units = 1 if unit_trade == 0 & unit_production == 9     //kg & kg H2O2
replace index_units = 1 if unit_trade == 0 & unit_production == 10    //kg & kg K2O
replace index_units = 1 if unit_trade == 0 & unit_production == 11	  //kg & kg KOH
replace index_units = 1 if unit_trade == 0 & unit_production == 12	  //kg & kg N
replace index_units = 1 if unit_trade == 0 & unit_production == 13	  //kg & kg NaOH
replace index_units = 1 if unit_trade == 0 & unit_production == 14	  //kg & kg P2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 15	  //kg & kg met.am.
replace index_units = 1 if unit_trade == 0 & unit_production == 16	  //kg & kg/net eda
replace index_units = 1 if unit_trade == 0 & unit_production == 17	  //kg & kg C5H14ClNO
replace index_units = 1 if unit_trade == 0 & unit_production == 25 	  //kg & kg Al2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 26    //kg & kg B2O3
replace index_units = 1 if unit_trade == 0 & unit_production == 27	  //kg & kg BaCO3
replace index_units = 1 if unit_trade == 0 & unit_production == 28	  //kg & kg Cl  
replace index_units = 1 if unit_trade == 0 & unit_production == 29	  //kg & kg F
replace index_units = 1 if unit_trade == 0 & unit_production == 30	  //kg & kg HCl
replace index_units = 1 if unit_trade == 0 & unit_production == 31	  //kg & kg HF
replace index_units = 1 if unit_trade == 0 & unit_production == 32	  //kg & kg K2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 33	  //kg & kg Na2CO3
replace index_units = 1 if unit_trade == 0 & unit_production == 34	  //kg & kg Na2S2O5
replace index_units = 1 if unit_trade == 0 & unit_production == 35	  //kg & kg PbO
replace index_units = 1 if unit_trade == 0 & unit_production == 36	  //kg & kg S
replace index_units = 1 if unit_trade == 0 & unit_production == 37	  //kg & kg SO2
replace index_units = 1 if unit_trade == 0 & unit_production == 38	  //kg & kg SiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 39	  //kg & kg TiO2
replace index_units = 1 if unit_trade == 0 & unit_production == 40	  //kg & kg act.subst
sort prccode
by prccode: gen nr_observations = _N
by prccode: egen nr_equal_units = sum(index_units)
by prccode: keep if nr_observations == nr_equal_units
rename C_production imports_value_pc8 
rename C_production_quantity imports_quantity_pc8
keep prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8 unit_trade unit_production
order prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant partner imports_value_pc8 imports_quantity_pc8
sort prccode year quarter declarant  
by prccode year quarter declarant: keep if _n == 1 
replace partner = declarant
save estimation\1_data_format\production_second_4_2007.dta, replace


use estimation\1_data_format\data_4_2007.dta, clear
merge m:1 prccode using estimation\1_data_format\pc8_first_4_2007.dta
drop if _merge == 3
drop _merge
merge m:1 prccode using estimation\1_data_format\pc8_second_4_2007.dta
drop if _merge == 3
drop _merge
sort prccode
egen pc8_index = group(prccode)
by prccode: keep if _n == 1 
keep prccode pc8_index
save estimation\1_data_format\pc8_third_4_2007.dta, replace


use estimation\1_data_format\imports_first_4_2007.dta, clear
append using estimation\1_data_format\imports_second_4_2007.dta
append using estimation\1_data_format\production_first_4_2007.dta
append using estimation\1_data_format\production_second_4_2007.dta
sort prccode year quarter declarant partner
gen price_pc8 = imports_value_pc8 / imports_quantity_pc8


drop unit_production
sort prccode year quarter declarant partner
save estimation\1_data_format\data_summary_4_2007.dta, replace


rm estimation\1_data_format\data_4_2007.dta
rm estimation\1_data_format\imports_first_4_2007.dta
rm estimation\1_data_format\imports_second_4_2007.dta
rm estimation\1_data_format\pc8_first_4_2007.dta
rm estimation\1_data_format\pc8_first_alt_4_2007.dta
rm estimation\1_data_format\pc8_second_4_2007.dta
rm estimation\1_data_format\pc8_third_4_2007.dta
rm estimation\1_data_format\production_first_4_2007.dta
rm estimation\1_data_format\production_second_4_2007.dta
